//
//  JGPhotoManager.m
//  JGRider
//
//  Created by 单启志 on 2018/2/8.
//  Copyright © 2018年 ffq. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "JGPhotoManager.h"
#import "cameraHelper.h"

#import <TZImageManager.h>
#import <TZLocationManager.h>

@interface JGPhotoManager()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,TZImagePickerControllerDelegate>
@property(nonatomic,weak)UIViewController *controller;
@property(nonatomic,copy)void(^callBlock)(UIImage *);
@end
@implementation JGPhotoManager
{
     NSMutableArray *_selectedAssets;
}
+(instancetype)share{
    static dispatch_once_t onceToken;
    static JGPhotoManager *manage;
    dispatch_once(&onceToken, ^{
        manage=[[JGPhotoManager alloc]init];
    });
    return manage;
}

//相册
-(void)photoWithMaxImagesCount:(NSInteger)count Controller:(UIViewController*)vc callBack:(void(^)(NSArray<UIImage *> *photos, NSArray *assets))block{

    self.controller=vc;
    if (![cameraHelper checkPhotoLibraryAuthorizationStatus]) {
        return;
    }
    TZImagePickerController *imagePickerVc=[[TZImagePickerController alloc]initWithMaxImagesCount:count delegate:self];
    imagePickerVc.naviBgColor=HEXCOLOR(0xfea129);
    imagePickerVc.alwaysEnableDoneBtn=YES;
    if (count==1) {//选一张图的时候支持裁剪
        imagePickerVc.showSelectBtn = NO;
        imagePickerVc.allowCrop = YES;
        // 设置竖屏下的裁剪尺寸
        NSInteger left = 0;
        NSInteger widthHeight = self.controller.view.width - 2 * left;
        NSInteger top = (self.controller.view.height - widthHeight) / 2;
        imagePickerVc.cropRect = CGRectMake(left, top, widthHeight, widthHeight);
    }
    imagePickerVc.selectedAssets=_selectedAssets;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        _selectedAssets=assets.mutableCopy;
        if (block) {
            block(photos,assets);
        }
    }];

    [vc presentViewController:imagePickerVc animated:YES completion:nil];
    
}

//预览
+(void)previewWithController:(UIViewController*)vc WithSelectedAssets:(NSMutableArray*)assets selectedPhotos:(NSMutableArray<UIImage*>*)photos index:(NSInteger)index{
    
    TZImagePickerController *imagePickerVc =[[TZImagePickerController alloc]initWithSelectedAssets:assets selectedPhotos:photos index:index];
    [vc presentViewController:imagePickerVc animated:YES completion:nil];
}

//拍照
-(void)CameraWithController:(UIViewController*)vc callBack:(void(^)(UIImage *image))block{
    if (![cameraHelper checkCameraAuthorizationStatus]) {
        return;
    }
    self.controller=vc;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;//设置可编辑
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self.controller presentViewController:picker animated:YES completion:nil];//进入照相界面
    self.callBlock = block;
}


#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *pickerImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    if (self.callBlock) {
        self.callBlock(pickerImage);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


-(void)selectedAssets:(NSArray<UIImage*>*)arr{
    _selectedAssets=arr.mutableCopy;
}
@end
